//
//  LoginHandler.swift
//  ServerPackageDescription
//
//  Created by 程平 on 2017/10/21.
//

import Foundation
import PerfectLib
import PerfectHTTP
extension Handler {
    

    static  func login(request:HTTPRequest, response:HTTPResponse)  {
      
            print("登陆接口")
            
            guard let name = checkParmeter(key: "userName", request, response),
                let password = checkParmeter(key: "password", request, response) else{
                return
            }
            if name == "admin" && password == "123456" {
                let session = sessionManager.start(request, userId: 1001)
                let data:[String : Any] = ["token":session.token,"expire":(session.updated+session.idle),"userId":1001]
                let result = ResponseData.jsonData(resultCode: ResultCode.success, data: data)
                response.setBody(string: result)
              
            }else{
                let result = ResponseData.jsonData(resultCode: ResultCode.userNameOrPwdError, data: nil)
                response.setBody(string: result)
            }
            response.setHeader(.contentLength, value: "\(response.bodyBytes.count)")
            response.setHeader(.contentType, value: ResponseData.contentType)
            response.completed()
        
    }
    static  func register(request:HTTPRequest, response:HTTPResponse)  {
        
            print("注册接口")
            print(request.queryParams)
            response.setHeader(HTTPResponseHeader.Name.contentType, value: "text/plain;charset=utf-8")
             response.setBody(string: "注册响应")
            response.completed()
        
    }
    
    static func getPwdBack(request:HTTPRequest, response:HTTPResponse) {
       
            print("找回密码接口")
            print(request.queryParams)
            response.setHeader(HTTPResponseHeader.Name.contentType, value: "text/plain;charset=utf-8")
            response.setBody(string: "找回密码响应")
            response.completed()
        
    }
    
    static func getUserInfo() -> RequestHandler{
        return {
            request, response in
            print("获取用户信息接口")
            print(request.queryParams)
            response.setHeader(HTTPResponseHeader.Name.contentType, value: "text/plain;charset=utf-8")
            response.setBody(string: "获取用户信息接口响应")
            response.completed()
        }
    }
}
